package com.italicatab;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.italicatab.Product.GetAllCategories;
import com.italicatab.Product.LazyAdapterGrid;

import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.AdapterView.OnItemClickListener;
import it.sephiroth.android.library.widget.HListView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ProductDetail extends Activity implements OnClickListener,
		OnItemClickListener {
	static final String URL = "http://makemyonlineshop.com/demo/italica/api/v1/product_group_image/";

	Animation Zoomin;
	private boolean flag = false;
	private ImageView iv_right, iv_close, iv_tiles;
	RelativeLayout rl_whole, rl_list;
	HListView listview;
	String PRO_ID;
	ProgressBar PROGRESS_BAR;
	NetworkInfo nwinfo;
	TextView TILE_NAME;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_detail);
		// ActivitySwitcher.animationIn(findViewById(R.id.container),
		// getWindowManager());
		PRO_ID = getIntent().getExtras().getString("id");
		init();
		if (isOnline()) {
			General.ProductsDetail = new ArrayList<HashMap<String, String>>();
			new GetAllProducts().execute();
		} else {
			Toast.makeText(getApplicationContext(),
					"Please Check Internet connection", Toast.LENGTH_SHORT)
					.show();
		}
	}

	private void init() {
		listview = (HListView) findViewById(R.id.hListView1);
		Zoomin = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.zoom);
		PROGRESS_BAR = (ProgressBar) findViewById(R.id.progressBar1);
		iv_tiles = (ImageView) findViewById(R.id.iv_tiles);
		iv_close = (ImageView) findViewById(R.id.iv_close);
		rl_whole = (RelativeLayout) findViewById(R.id.rl_whole);
		rl_list = (RelativeLayout) findViewById(R.id.rl_list);

		TILE_NAME = (TextView) findViewById(R.id.tv_tiles_name);
		iv_right = (ImageView) findViewById(R.id.iv_right);

		iv_right.setOnClickListener(this);
		listview.setOnItemClickListener(this);
		iv_close.setOnClickListener(this);

	}

	public class GetAllProducts extends AsyncTask<Void, Integer, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			PROGRESS_BAR.setVisibility(View.VISIBLE);
		}

		@Override
		protected String doInBackground(Void... params) {
			XMLParser parser = new XMLParser();
			String xml = parser.getXmlFromUrl(URL + PRO_ID); // getting
			// XML
			// Log.e("magnum", "product data is"+xml);
			Document doc = parser.getDomElement(xml); // getting DOM element
			doc.getDocumentElement().normalize();

			NodeList nl = doc.getElementsByTagName("product");
			// looping through all item nodes <item>
			for (int i = 0; i < nl.getLength(); i++) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				Element e = (Element) nl.item(i);
				// adding each child node to HashMap key => value
				map.put("id_products", parser.getValue(e, "id_products"));
				map.put("productname", parser.getValue(e, "productname"));
				map.put("categoryname", parser.getValue(e, "categoryname"));
				System.out.println(parser.getValue(e, "src"));
				map.put("src", parser.getValue(e, "src"));
				map.put("largeimage", parser.getValue(e, "largeimage"));
				map.put("fourtiles", parser.getValue(e, "fourtiles"));

				// adding HashList to ArrayList
				General.ProductsDetail.add(map);
			}
			System.out.println("size is " + General.ProductsDetail.size());

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			PROGRESS_BAR.setVisibility(View.GONE);
			listview.setAdapter(new HAdapter());
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		// ActivitySwitcher.animationIn(findViewById(R.id.container),
		// getWindowManager());

		super.onResume();

	}

	// private static String[] dataObjects = new String[] { "Wood Scape",
	// "Wood Scape", "Wood Scape", "Wood Scape", "Wood Scape",
	// "Wood Scape" };

	private class HAdapter extends BaseAdapter {

		public HAdapter() {
			super();
		}

		public int getCount() {
			return General.ProductsDetail.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {

			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.viewitem, null);
			}

			// View retval =
			// LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem,
			// null);
			ImageView iv = (ImageView) convertView.findViewById(R.id.image);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			ImageView ivzoom = (ImageView) convertView
					.findViewById(R.id.iv_zoom);
			ImageView ivsquar = (ImageView) convertView
					.findViewById(R.id.iv_square);

			title.setText(General.ProductsDetail.get(position).get(
					"productname"));
			String url = General.ProductsDetail.get(position).get("src");
			iv.setTag(url);
			final clsLazyLoad lazy = new clsLazyLoad(ProductDetail.this, true);
			lazy.DisplayImage(url, ProductDetail.this, iv);
			ivzoom.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					rl_whole.setVisibility(View.VISIBLE);
					String url = General.ProductsDetail.get(position).get(
							"largeimage");
					iv_tiles.setTag(url);
					TILE_NAME.setText(General.ProductsDetail.get(position).get(
							"productname"));
					lazy.DisplayImage(url, ProductDetail.this, iv_tiles);
					iv_tiles.startAnimation(Zoomin);
				}
			});
			ivsquar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					rl_whole.setVisibility(View.VISIBLE);
					String url = General.ProductsDetail.get(position).get(
							"fourtiles");
					TILE_NAME.setText(General.ProductsDetail.get(position).get(
							"productname"));
					iv_tiles.setTag(url);
					lazy.DisplayImage(url, ProductDetail.this, iv_tiles);
					iv_tiles.startAnimation(Zoomin);
				}
			});

			/*
			 * Button button = (Button)
			 * convertView.findViewById(R.id.clickbutton);
			 * button.setOnClickListener(mOnButtonClicked);
			 */

			return convertView;
		}

	};

	private Animation outToLeftAnimation() {

		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(800);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		outtoLeft.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				listview.setVisibility(View.GONE);
				iv_right.setImageResource(R.drawable.arrow_left);

			}
		});
		return outtoLeft;
	}

	private Animation outToRightAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(800);
		inFromLeft.setInterpolator(new AccelerateInterpolator());

		return inFromLeft;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// rl_whole.setVisibility(View.VISIBLE);
		// iv_tiles.startAnimation(Zoomin);

	}

	@Override
	public void onClick(View v) {
		if (v == iv_right) {
			if (flag) {
				flag = false;
				// slideToRight(listview);
				rl_list.startAnimation(outToLeftAnimation());
			} else {
				flag = true;

				listview.setVisibility(View.VISIBLE);
				rl_list.startAnimation(outToRightAnimation());

				iv_right.setImageResource(R.drawable.arrow_right);
				// slideToLeft(listview);
			}
		} else if (v == iv_close) {
			rl_whole.setVisibility(View.GONE);
		}
	}

	private boolean isOnline() {

		// it provides Internet information
		ConnectivityManager conmanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		nwinfo = conmanager.getActiveNetworkInfo();
		return nwinfo != null;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.gc();
	}
	/*
	 * private Animation outToLeftAnimation() {
	 * 
	 * Animation outtoLeft = new TranslateAnimation( Animation.ABSOLUTE, 0.02f,
	 * Animation.ABSOLUTE, -1.0f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE,
	 * 0.0f); outtoLeft.setDuration(500); outtoLeft.setInterpolator(new
	 * AccelerateInterpolator()); return outtoLeft; }
	 * 
	 * private Animation outToRightAnimation() { Animation inFromLeft = new
	 * TranslateAnimation( Animation.RELATIVE_TO_SELF, -1.0f,
	 * Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
	 * Animation.RELATIVE_TO_SELF, 0.0f); inFromLeft.setDuration(500);
	 * inFromLeft.setInterpolator(new AccelerateInterpolator()); return
	 * inFromLeft; }
	 */

}
